<?php
namespace Admin\Controller;

use Think\Controller;

/**
 * 外包客服外呼中心
 */
class OutboundController extends CommonController
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 用户调查外呼列表
     */
    public function outbound(){
        $this->display();
        die;
    }
    /**
     * 显示认证信息
     */
    public function show_verify()
    {
        $vid = I('vid');
        $field = 'taobao_verify,credit_verify,shebao_verify,jd_verify,mobile_verify,contact_verify,zhima_verify,work_cert';
        $v = M('user', '', 'USER')->where(['vid' => $vid])->field($field)->find();

        $data = array();
        if ($v['taobao_verify'] == 1) {

            $data[] = '淘宝未认证';
        }
        if ($v['credit_verify'] == 1) {

            $data[] = '信用卡未认证';
        }
        if ($v['shebao_verify'] == 1) {

            $data[] = '社保未认证';
        }
        if ($v['jd_verify'] == 1) {

            $data[] = '京东未认证';
        }
        if ($v['mobile_verify'] == 1) {

            $data[] = '运营商未认证';
        }
        if ($v['contact_verify'] == 0) {

            $data[] = '联系人未认证';
        }
        if ($v['zhima_verify'] == 0) {

            $data[] = '芝麻未认证';
        }
        if ($v['work_cert'] == 1) {

            $data[] = '工作未认证';
        }
        $this->assign(array(
            'data' => $data,

        ));
        $this->display();
        die;

    }

    /**
     * 快速回复标签
     */
    public function Quick_answer()
    {
        if (empty($_POST)) {
            $vid = I('get.vid');
            $url_type = I('get.url_type');
            $data = M('kefu_quick_answer', '', 'KEFU')->where(['type' => 0])->field('title,id')->select();
            $this->assign(array(
                'data' => $data,
                'vid' => $vid,
                'url_type' => $url_type
            ));
            $this->display();
            die;
        }
        $title = I('title');
        $arr = array('title' => $title, 'ctime' => time(), 'kf_id' => $_SESSION['id']);
        $add = M('kefu_quick_answer', '', 'KEFU')->add($arr);
        if ($add) {
            add_log('添加快速回复' . $add);
            $this->ajax('1001', '添加成功');
            die;
        }
        $this->ajax('1002', '添加失败');
        die;
    }
    /**
     * 回复记录
     */
    public function Remarks()
    {
        if (empty($_POST)) {
            $vid = I('get.vid');
            $url_type = I('get.url_type');
            $this->assign(array(
                'vid' => $vid,
                'url_type' => $url_type
            ));
            $this->display();
            die;
        }
    }
    /**
     * 回复记录
     */
    public function outbound_result()
    {
        $vid = I('vid');
        $type1 = I('type1');
        $content = I('content');
        $arr = array('admin_name' => $_SESSION['name'], 'content' => $content, 'ctime' => time(), 'vid' => $vid);
        $add = M('user_outbound_message', '', 'KEFU')->add($arr);
        if ($add) {
            //修改回复状态 已经沟通
            M('send_outbound_user', '', 'KEFU')->where(['vid' => $vid])->setfield('status', '2');
            if ($type1 == '2') {//无机型
                M('no_mobilefirm_no_cert', '', 'KEFU_FK')->where(['vid' => $vid])->setfield('is_call', 1);
            } else if ($type1 == '4') {//有机型
                M('mobilefirm_no_cert', '', 'KEFU_FK')->where(['vid' => $vid])->setfield('is_call', 1);
            } else if ($type1 == '3') {//认证通过未借款
                M('cert_no_borrow', '', 'KEFU_FK')->where(['vid' => $vid])->setfield('is_call', 1);
            }
            add_log('对用户' . $vid . '外呼备注ID' . $add);
            $this->ajax('1001', '备注成功' . $type1 . '12');
            die;
        }
        $this->ajax('1002', '备注失败');
        die;
    }

    /**
     * 风控未通过用户列表
     */
    public function get_survey()
    {
        $url_type = I('type',1);//1 风控未通过用户列表 2 无机型注册未认证 3 通过未借款  4 有机型注册未认证
        $url_type_status = I('type_status',0);//0 全部 1没沟通 2 沟通过 3 已经解决 4 去认证  用户列表沟通状态
        $num=10;
        $n = I('page', 1);
        $page=($n-1)*$num;
        $where['b.user_type'] = array('eq', 2);
        $type1 = $url_type;
        if ($type1 == '1') {
            $where['b.type'] = array('eq', '1');
        } else if ($type1 == '2') {
            $where['b.type'] = array('eq', '2');
            if ($url_type_status != 4) {
                $where['a.cert_status'] = array('eq', '1');
            }
        } else if ($type1 == '3') {
            $where['b.type'] = array('eq', '3');
        } else if ($type1 == '4') {
            $where['b.type'] = array('eq', '4');
            if ($url_type_status != 4) {
                $where['a.cert_status'] = array('eq', '1');
            }
        }

        if ($url_type_status == '1') {//未沟通
            $where['b.status'] = array('eq', '1');
        } else if ($url_type_status == '2') {//已经沟通
            $where['b.status'] = array('eq', '2');
        } else if ($url_type_status == '4') {
            $where['b.status'] = array('eq', '2');
            $where['a.usermsg_verify'] = array('eq', '1');
            $where['a.contact_verify'] = array('eq', '1');
            $where['a.zhima_verify'] = array('eq', '1');
            $where['a.mobile_verify'] = array('eq', '3');
        }
        if ($_SESSION['role_id'] == '7'){
            $where['b.out_kf_id'] = array('eq', $_SESSION['id']);
            $where['b.user_type'] = array('eq', '2');
        }
        $field = 'a.taobao_verify,a.credit_verify,a.shebao_verify,a.jd_verify,a.mobile_verify,a.contact_verify,a.zhima_verify,a.work_cert,a.quota,a.vid,a.id,a.mobile,a.truename,a.cert_status,a.sex,a.ctime';
        if ($url_type_status == '3' && $url_type == '2') {// 无机型注册未认证 已经解决
            $where['a.cert_status'] = array('eq', '2');
            $where['b.status'] = array('eq', '2');
        } else if ($url_type_status == '3' && $url_type == '4') {// 有机型注册未认证 已经解决
            $where['a.cert_status'] = array('eq', '2');
            $where['b.status'] = array('eq', '2');
        } else if ($url_type_status == '3' && $url_type == '1') { //分控未通过  已经解决     $where['b.status']=array('eq',2);
            $where['a.cert_status'] = array('eq', 2);
        }

        if ($url_type == '3') {
            $field = 'c.call_user_level,a.taobao_verify,a.credit_verify,a.shebao_verify,a.jd_verify,a.mobile_verify,a.contact_verify,a.zhima_verify,a.work_cert,a.quota,a.vid,a.id,a.mobile,a.truename,a.cert_status,a.sex,a.ctime';
           if($url_type_status=='3'){
               $where['b.status'] = array('eq', '2');
               $sum = M('send_outbound_user', '', 'KEFU')->join('b join dxd_user.user a on b.vid=a.vid')->join('join dxd_caiwu.user_borrow d on a.vid=d.vid')->field('b.vid')->distinct('b.vid')->where($where)->select();
               $count=count($sum);
               $user = M('send_outbound_user', '', 'KEFU')->join('b join dxd_user.user a on b.vid=a.vid')->join('join dxd_fk.cert_no_borrow c on  b.vid=c.vid')->join('join dxd_caiwu.user_borrow d on a.vid=d.vid')->LIMIT($page, $num)->distinct('b.vid')->order('c.call_user_level asc')->field($field)->where($where)->select();
           }else{
               $user = M('send_outbound_user', '', 'KEFU')->join('b join dxd_user.user a on b.vid=a.vid')->join('join dxd_fk.cert_no_borrow c on  b.vid=c.vid')->distinct('b.vid')->LIMIT($page, $num)->order('c.call_user_level asc')->field($field)->where($where)->select();
               $count = M('send_outbound_user', '', 'KEFU')->join('b join dxd_user.user a on b.vid=a.vid')->where($where)->count();
           }

        } else if ($url_type == '2') {
            $count = M('send_outbound_user', '', 'KEFU')->join('b  join dxd_user.user a on b.vid=a.vid')->where($where)->count();
            $field = 'a.vid,a.id,a.mobile,a.truename,a.cert_status,a.sex,a.ctime';//,
            $user = M('send_outbound_user', '', 'KEFU')->join('b  join dxd_user.user a on b.vid=a.vid')->where($where)->field($field)->LIMIT($page, $num)->order('b.id asc')->select();

        } else if ($url_type == '4') {
            $count = M('send_outbound_user', '', 'KEFU')->join('b  join dxd_user.user a on b.vid=a.vid')->join('join dxd_fk.mobilefirm_no_cert c on b.vid=c.vid')->where($where)->count();
            $field = 'a.vid,a.id,a.mobile,a.truename,a.cert_status,a.sex,a.ctime,c.call_level as call_user_level';//
            $user = M('send_outbound_user', '', 'KEFU')->join('b  join dxd_user.user a on b.vid=a.vid')->join('join dxd_fk.mobilefirm_no_cert c on b.vid=c.vid')->where($where)->field($field)->LIMIT($page, $num)->order('c.call_level asc')->select();
        } else {
            $user = M('send_outbound_user', '', 'KEFU')->join('b join dxd_user.user a on b.vid=a.vid')->where($where)->field($field)->LIMIT($page, $num)->order('a.ctime desc')->select();
            $count = M('send_outbound_user', '', 'KEFU')->join('b join dxd_user.user a on b.vid=a.vid')->where($where)->count();
        }
        foreach ($user as $k => $v) {
            //呼叫等级为空
            if(empty($v['call_user_level'])){
                $user[$k]['call_user_level']="-";
            }
            // 手机型号
            $mobilefirm = M('user_login', '', 'USER')->where(['vid' => $v['vid']])->field('mobilefirm')->find();
            $user[$k]['mobilefirm'] = $mobilefirm['mobilefirm'];
            //最近回复内容
            $feedback = M('user_outbound_message', '', 'KEFU')->where(['vid' => $v['vid']])->field('content')->limit(1)->order('ctime desc')->find();
            if (!empty($feedback)) {
                $user[$k]['feedback_status'] = '1';//已经备注过
                $user[$k]['feedback_content'] = $feedback['content'];//备注内容
            } else {
                $user[$k]['feedback_status'] = '0';//没有备注过
                $user[$k]['feedback_content'] = '-';//备注内容
            }
            $borrow_time = M('user_borrow', '', 'CAIWU')->where(['vid' => $v['vid']])->field('ctime,id')->order('id desc')->find();
            if (empty($borrow_time)) {
                $user[$k]['borrow_time'] = '-';
            } else {
                $user[$k]['borrow_time'] = date('Y-m-d H:i', $borrow_time['ctime']);
            }
            if ($v['cert_status'] == '1') {
                $user[$k]['cert_status'] = '未认证';
            } else if ($v['cert_status'] == '2') {
                $user[$k]['cert_status'] = '已通过';
            } else if ($v['cert_status'] == '3') {
                $user[$k]['cert_status'] = '未通过';
            }

            if ($v['sex'] == '0') {
                $user[$k]['sex'] = '未知';
            } else if ($v['sex'] == '1') {
                $user[$k]['sex'] = '男';
            } else if ($v['sex'] == '2') {
                $user[$k]['sex'] = '女';
            }
            $user[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
            //隐藏手机号
            $first=substr($v['mobile'],0,3);
            $end=substr($v['mobile'],7,4);
            $user[$k]['user_mobile']=$first.'*****'.$end;

            if(empty($v['truename'])){
                $user[$k]['truename']='----';
            }
        }
        $count=intval($count);
        $tol=ceil($count/$num);
        $this->ajaxReturn([ 'code'=>'1001', 'data' => $user, 'url_type' => $url_type, 'count'=>$count, 'total' => $tol]);
        die;
    }

    public function test(){
        $this->ajaxReturn([ 'code'=>'1001', 'data' => '23232']);
        die;
    }


    /**
     * 详情
     */

    public function list_details()
    {
        $vid = I('get.vid');
        $data = M('user_outbound_message', '', 'KEFU')->join('b join dxd_user.user a on b.vid=a.vid')->where(['b.vid' => $vid])->field('b.id,b.admin_name,b.content,b.ctime,b.vid,a.truename,a.mobile')->select();

        foreach ($data as $k => $v) {
            $data[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
        }

        $url_type = I('url_type');
        $type_status = I('type_status');
        $this->page = I('page');
        $this->assign(array(
            'data' => $data,
            'vid' => $vid,
            'url_type' => $url_type,
            'type_status' => $type_status
        ));

        $this->display();
    }

    /**
     * 详情
     */

    public function outbound_details()
    {
        $vid = I('vid');
        $data = M('user_outbound_message', '', 'KEFU')->join('b join dxd_user.user a on b.vid=a.vid')->where(['b.vid' => $vid])->field('b.id,b.admin_name,b.content,b.ctime,b.vid,a.truename,a.mobile')->select();

        foreach ($data as $k => $v) {
            $data[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
            $first=substr($v['mobile'],0,3);
            $end=substr($v['mobile'],7,4);
            $data[$k]['user_mobile']=$first.'****'.$end;
        }
        $url_type = I('url_type');
        $type_status = I('type_status');
        $this->page = I('page');
        $this->assign(array(
            'data' => $data,
            'vid' => $vid,
            'url_type' => $url_type,
            'type_status' => $type_status
        ));
        $this->display();
    }

    /**
     * 编辑内容
     */
    public function edit_content()
    {
//        if (empty($_POST)) {
//            $id = I('get.id');
//            $content = M('user_outbound_message', '', 'KEFU')->where(['id' => $id])->field('content,vid')->find();
//            $this->assign(array(
//                'content' => $content['content'],
//                'id' => $id,
//                'vid' => $content['vid']
//            ));
//            $this->display();
//        } else {
            $id = I('id');
            $content = I('edit_content');

            $info = M('user_outbound_message', '', 'KEFU')->where(['id' => $id])->setfield('content', $content);
            if ($info) {
                add_log('修改外呼记录' . $id);
                $this->ajax('1001', '修改成功');
                die;
            }
            $this->ajax('1002', '修改失败');
            die;
//        }

    }



}